﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
using Assesment.Library.DomainObject.Administration.UserManagement;
using Assesment.Web.Support;
using Assesment.Web;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using Training.Library.DataAccess.Administration.Catalog;
using Training.Library.DomainObject.Administration.Catalog;
using Assesment.Library.DataAccess.Transaction;
using Assesment.Library.DomainObject.Transaction;
using Assesment.Library.DataAccess.Administration.Catalog;
using Assesment.Library.DomainObject.Administration.Catalog;

namespace Assesment.Web.Forms.Transaction
{
    public partial class AssesmentPositionForm : BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {

            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            txtTglAssesment.Text = DateTime.Now.ToString("dd/MM/yyyy");
            if (!Page.IsPostBack)
            {
                try
                {
                    DepartmentRepo DepartmentDal = new DepartmentRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);

                    List<Department> departments = DepartmentDal.GetList();
                    var query = departments.OrderBy(x => x.DepartmentDesc);
                    departments = query.ToList();
                    ddlDepartemen.Items.Clear();
                    departments.ForEach(delegate(Department department)
                    {
                        ddlDepartemen.Items.Add(new ListItem(department.DepartmentDesc, department.DeptID.ToString()));
                    });

                    ddlDepartemen.Items.Insert(0, new ListItem("-- pilih departemen --", string.Empty));


                    LocationRepo LocationDal = new LocationRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);

                    List<Location> listlokasi = LocationDal.GetList();
                    var queryLokasi = listlokasi.OrderByDescending(x => x.LocationID);
                    listlokasi = queryLokasi.ToList();
                    ddlLokasi.Items.Clear();
                    listlokasi.ForEach(delegate(Location objLokasi)
                    {
                        ddlLokasi.Items.Add(new ListItem(objLokasi.Name, objLokasi.LocationID.ToString()));
                    });

                    ddlLokasi.Items.Insert(0, new ListItem("-- pilih lokasi --", string.Empty));

                    KaryawanRepo KaryawanDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                    List<Karyawan> listKaryawan = KaryawanDal.GetList();
                    var queryKaryawan = listKaryawan.OrderBy(x => x.NamaKaryawan);
                    listKaryawan = queryKaryawan.ToList();
                    ddlNIK.Items.Clear();
                    listKaryawan.ForEach(delegate(Karyawan objKaryawan)
                    {
                        ddlNIK.Items.Add(new ListItem(objKaryawan.NIK+" - " + objKaryawan.NamaKaryawan, objKaryawan.NIK.ToString()));
                    });

                    ddlNIK.Items.Insert(0, new ListItem("-- pilih NIK --", string.Empty));

                    TotalFitRepo TotalFitDal = new TotalFitRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                    List<TotalFit> listTotal = TotalFitDal.GetList();
                    var queryTotal = listTotal.OrderBy(x => x.FitStart);
                    listTotal = queryTotal.ToList();
                    ddlTotalFit.Items.Clear();
                    listTotal.ForEach(delegate(TotalFit objTotal)
                    {
                        ddlTotalFit.Items.Add(new ListItem("Core "+objTotal.FitStart + "% - Managerial " + objTotal.FitEnd+"%", objTotal.FitID.ToString()));
                    });

                    LeadershipRepo LeadershipDal = new LeadershipRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                    List<Leadership> listLeadership = LeadershipDal.GetList();
                    //var queryTotal = listTotal.OrderBy(x => x.FitStart);
                    //listTotal = queryTotal.ToList();
                    ddlLeadership.Items.Clear();
                    listLeadership.ForEach(delegate(Leadership objLeadership)
                    {
                        ddlLeadership.Items.Add(new ListItem(objLeadership.Code.ToString() + " - " + objLeadership.Msdt , objLeadership.LeadershipID.ToString()));
                    });

                    //ddlNIK.Items.Insert(0, new ListItem("-- pilih NIK --", string.Empty));

                    PersonalityRepo PersonalityDal = new PersonalityRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                    List<Personality> listPersonality = PersonalityDal.GetList();
                    //var queryTotal = listTotal.OrderBy(x => x.FitStart);
                    //listTotal = queryTotal.ToList();
                    ddlPersonality.Items.Clear();
                    listPersonality.ForEach(delegate(Personality objPersonality)
                    {
                        ddlPersonality.Items.Add(new ListItem(objPersonality.Code.ToString() + " - " + objPersonality.Disc, objPersonality.PersonalityID.ToString()));
                    });

                    if (id > 0) // Edit an existing item
                    {

                        hfID.Value = id.ToString();

                        AssesmentPositionRepo AssesmentPositionDal = new AssesmentPositionRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                        AssesmentPosition obj = AssesmentPositionDal.GetById(id);
                        if (obj != null)
                        {
                            ddlNIK.Items.FindByValue(obj.NIK.ToString()).Selected = true;

                            //KaryawanRepo KaryawanDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                            Karyawan objKaryawan = KaryawanDal.GetById(obj.NIK);
                            if (objKaryawan != null)
                                txtNama.Text = objKaryawan.NamaKaryawan;


                            PositionRepo PositionDal = new PositionRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);
                            Position objPosition = PositionDal.GetById(Convert.ToInt32(objKaryawan.PositionID));
                            if (objPosition != null)
                            {
                                txtJabatan.Text = objPosition.PositionName;
                                //ddlLokasi.Items.FindByValue(objPosition.LocationID.ToString()).Selected = true;

                            }
                            Position objPositionAssesment = PositionDal.GetById(Convert.ToInt32(obj.PositionIDAssesment));
                            if(objPositionAssesment!=null)
                                ddlLokasi.Items.FindByValue(objPositionAssesment.LocationID.ToString()).Selected = true;

                            ////LocationRepo LocationDal = new LocationRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);
                            //Location objLocation = LocationDal.GetById(Convert.ToInt32(objPosition.LocationID));
                            //if(objLocation!=null)
                            //    txtLokasiKerja.Text = objLocation.Name;

                            CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                            Cabang objCabang = CabangDal.GetById(Convert.ToInt32(objKaryawan.CabangID));
                            if (objCabang != null)
                                txtLokasiKerja.Text = objCabang.NamaCabang;

                            txtTglAssesment.Text = obj.AssesmentDate.Value.ToString("dd/MM/yyyy");
                            ddlDepartemen.Items.FindByValue(obj.DepartmentIDAssesment.ToString()).Selected = true;

                            List<Position> listPosition = PositionDal.GetList();
                            var queryPosition = listPosition.Where(x => x.DepartmentID == obj.DepartmentIDAssesment);
                            listPosition = queryPosition.ToList();
                            ddlJabatanTujuan.Items.Clear();
                            listPosition.ForEach(delegate(Position objData)
                            {
                                ddlJabatanTujuan.Items.Add(new ListItem(objData.PositionName, objData.PositionID.ToString()));
                            });

                            ddlJabatanTujuan.Items.Insert(0, new ListItem("-- pilih jabatan --", string.Empty));

                            ddlJabatanTujuan.Items.FindByValue(obj.PositionIDAssesment.ToString()).Selected = true;
                            ddlTotalFit.Items.FindByValue(obj.TotalFitID.ToString()).Selected = true;
                            txtPenilaianIndividu.Text = obj.PersonalDescription;
                            ddlPersonality.Items.FindByValue(obj.PersonalityID.ToString()).Selected = true;
                            ddlLeadership.Items.FindByValue(obj.LeadershipID.ToString()).Selected = true;
                            
                            
                        }
                        
                    }

                    GetPrivilege();
                }
                catch (Exception ex)
                {
                    ClientScript.RegisterClientScriptBlock(typeof(Page), "error", "alert('" + " Error :" + ex.Message + "');", true);
                    return;
                }


            }


        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    }
}